package com.example.laixunlong.control;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.laixunlong.R;
import com.example.laixunlong.database.MyDatabase;
import com.example.laixunlong.pojo.Json;
import com.example.laixunlong.tools.FileInOutPut;
import com.example.laixunlong.tools.HttpGetPost;
import com.example.laixunlong.tools.ImageInOutPut;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class FileInOutPut_shujvcunchu extends AppCompatActivity {
/*数据存储,一般使用四种方式:
*       SharedPreferences   共享参数
*       SQLite              android专用数据库
*       File-I/O            写入磁盘文件
*       static              全局静态常量
*/
    Button xml_button_1,xml_button_2,xml_button_3,xml_button_4,
        sql_button_1,sql_button_2,sql_button_3,sql_button_4,
        file_button_1,file_button_2,file_button_3,file_button_4,
        stat_button_1,stat_button_2,
        internet_button_1,internet_button_2,internet_button_3;

    EditText sql_input_1,sql_input_2,file_input_1,file_input_2;
    ImageView image_file_1;
    SharedPreferences preferences;
    String sql_path;
    MyDatabase sql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_inoutput_shujvcunchu);
        //初始化全局变量
        xml_button_1=findViewById(R.id.xml_button_1);
        xml_button_2=findViewById(R.id.xml_button_2);
        xml_button_3=findViewById(R.id.xml_button_3);
        xml_button_4=findViewById(R.id.xml_button_4);
        preferences=getSharedPreferences("xml_shiyan", Context.MODE_PRIVATE);//创建文件
        XMLsave();
        sql_button_1=findViewById(R.id.sql_button_1);
        sql_button_2=findViewById(R.id.sql_button_2);
        sql_button_3=findViewById(R.id.sql_button_3);
        sql_button_4=findViewById(R.id.sql_button_4);
        sql_input_1=findViewById(R.id.sql_input_1);
        sql_input_2=findViewById(R.id.sql_input_2);
        sql_path= getFilesDir()+"/sql_shiyan.db";
        SQLsave();
        file_button_1=findViewById(R.id.file_button_1);
        file_button_2=findViewById(R.id.file_button_2);
        file_button_3=findViewById(R.id.file_button_3);
        file_button_4=findViewById(R.id.file_button_4);
        file_input_1=findViewById(R.id.file_input_1);
        file_input_2=findViewById(R.id.file_input_2);
        FILEsave();
        stat_button_1=findViewById(R.id.stat_button_1);
        stat_button_2=findViewById(R.id.stat_button_2);
        Staticsave();
        image_file_1=findViewById(R.id.image_file_1);
        internet_button_1=findViewById(R.id.internet_button_1);
        internet_button_2=findViewById(R.id.internet_button_2);
        internet_button_3=findViewById(R.id.internet_button_3);
        InternetSave();
    }


/*共享参数  SharedPreferences=======================================================================*/
    public void XMLsave()
    {
        /*保存键值对*/
        xml_button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor edit=preferences.edit();
                edit.putString("string","能保存字符串");
                edit.putInt("int",120);
                edit.commit();//保存并关闭流
                /*
                String value=preferences.getString("name",null);//通过键名获取数据,第二个参数表示当不存在时,返回的数据
                */
            }
        });
        /*显示文件内全部数据*/
        xml_button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, ?> key_Value = (Map<String, ?>) preferences.getAll();//获取全部内容
                for (String key:key_Value.keySet()) {
                    Log.e(key, key_Value.get(key)+"");
                }
            }
        });
    }



/*数据库   SQLite==================================================================================*/
    public void SQLsave()
    {   //增
        sql_button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //SQLiteDatabase db=openOrCreateDatabase(sql_path,Context.MODE_PRIVATE,null);
                //deleteDatabase(sql_path);
                //获取数据库对象,或创建新的数据库
                Json json=new Json();
                json.setKey(sql_input_1.getText().toString());
                json.setValue(sql_input_2.getText().toString());
                /*
                sql.insert(json);//增
                sql.delete(json.getKey());//删
                sql.update(json);//改
                List<Json> l=sql.select();//查
                */
                long a=sql.insert(json);
                if(a>0){Log.e("查询结果","执行成功,变动"+a+"行");}else{Log.e("添加结果","添加失败");}
            }
        });
        sql_button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Json> l=sql.select();
                for (Json j:l)
                {Log.e(j.getKey(),j.getValue());}
            }
        });
    }
    @Override/*页面加载成功*/
    protected void onStart() {
        super.onStart();
        sql=MyDatabase.getInstance(this);//获取数据库实例,单例
        sql.openInput();
        sql.openOutput();
    }
    @Override/*页面撤离*/
    protected void onStop() {
        sql.Close();
        super.onStop();
    }


/*写入文件   File-I/O===============================================================================*/
    public void FILEsave()
    {
        //增
        file_button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path=FileInOutPut.getPrivatePath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                String old=FileInOutPut.getTXT(path);
                String key=file_input_1.getText().toString();
                String value=file_input_2.getText().toString();
                FileInOutPut.saveTXT(path,old+key+"[@]"+value+"\n");
            }
        });
        //查
        file_button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path=FileInOutPut.getPublicPath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                //String path=FileInOutPut.getAppPath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                //String path=FileInOutPut.getPrivatePath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                String value=FileInOutPut.getTXT(path);
                Log.e("内容为","\n"+value);
            }
        });
        //删
        file_button_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path=FileInOutPut.getPublicPath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                //String path=FileInOutPut.getAppPath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                //String path=FileInOutPut.getPrivatePath(FileInOutPut_shujvcunchu.this,"laixunlong.txt");
                FileInOutPut.deleteFile(path);
            }
        });
    }


    public void Staticsave()
    {

    }
    /*网络保存*/
    public void InternetSave()
    {
        /*保存网络上获取的图片*/
        internet_button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageInOutPut.saveImage(FileInOutPut_shujvcunchu.this,
                        "http://localhost:8081/image/books/books_18.png",
                        "new_name_18.png",
                        ImageInOutPut.ROUTE_app,
                        ImageInOutPut.Internet);
            }
        });
        /*获取本地图片*/
        internet_button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String p= FileInOutPut.getAppPath(FileInOutPut_shujvcunchu.this,"new_name_18.png");
                ImageInOutPut.getImage(p, image_file_1);
            }
        });
        /*打开相册选择图片*/
        internet_button_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
































}